package common

import (
	"bytes"
	"encoding/binary"
	"log"

	"github.com/pion/webrtc/v3"
)

// 信号类型
type ESigType uint8

const (
	ECandidate         ESigType = 0  //打洞信息
	ESdp               ESigType = 1  //连接信息
	EServerClientClose ESigType = 2  //服务客户端主动断开连接
	EClientClientClose ESigType = 3  //客户端关闭
	ERegClient         ESigType = 4  //注册客户端
	ELinkClient        ESigType = 5  //连接客户端
	EClientIdDublite   ESigType = 6  //重复ID
	ENoServerClient    ESigType = 7  //客户端连接的server不存在
	EBeginHole         ESigType = 8  //开始打洞
	ERegOK             ESigType = 9  //注册成功
	ECreateAnswer      ESigType = 10 //创建answer
	ECreateAnswerOK    ESigType = 11 //创建成功
	ELinkOK            ESigType = 12
	EOnly              ESigType = 13
	EOnlyOK            ESigType = 14
)

// 客户端类型
type EClientType uint8

const (
	EClientClient EClientType = 0
	EServerClient EClientType = 1
	EServer       EClientType = 3
)

type P2PSignal struct {
	ClientType EClientType //客户端类型
	SigType    ESigType    //信号类型
	ClientId   string      //客户端识别码
	TargetId   string      //目标Id
	WebrtcId   int32       //p2p连接Id
	Content    string      //打洞ip,端口  //webrtc连接信息，webrtc.SessionDescription
}

type HostPortPeer struct {
	RemoteHost string
	RemotePort string
	LocalHost  string
	LocalPort  string
}

// stun服务地址配置
var WebrtcConfig = webrtc.Configuration{
	ICEServers: []webrtc.ICEServer{
		{
			URLs: []string{"stun:stun.qq.com:3478"},
		},
		{
			URLs: []string{"stun:stun.voipbuster.com:3478"},
		},
		{
			URLs: []string{"stun:stun.sipgate.net:3478"},
		},
		{
			URLs: []string{"stun:stun.internetcalls.com:3478"},
		},
		{
			URLs: []string{"stun:stun.callwithus.com:3478"},
		},
		{
			URLs: []string{"stun:stun.voipstunt.com:3478"},
		},
		{
			URLs:           []string{"turn:43.138.80.3:3478"},
			Username:       "wangxue",
			Credential:     "wx2348765",
			CredentialType: webrtc.ICECredentialTypePassword,
		},
	},
	ICETransportPolicy: webrtc.ICETransportPolicyAll,
}

var DebugLog = true

func LogDebug(v ...any) {
	if DebugLog {
		log.Println(v...)
	}
}

//打包数据
func PackageData(id string, action string, content string) []byte {
	l1 := len(id)
	l2 := len(action)
	l3 := len(content)
	var buf bytes.Buffer
	binary.Write(&buf, binary.LittleEndian, uint32(l1))
	binary.Write(&buf, binary.LittleEndian, uint32(l2))
	binary.Write(&buf, binary.LittleEndian, uint32(l3))
	binary.Write(&buf, binary.LittleEndian, []byte(id))
	binary.Write(&buf, binary.LittleEndian, []byte(action))
	binary.Write(&buf, binary.LittleEndian, []byte(content))
	return buf.Bytes()
}

// 解包数据
func UnPackageData(data []byte) (string, string, string) {
	var l1, l2, l3 uint32
	buf := bytes.NewReader(data)
	binary.Read(buf, binary.LittleEndian, &l1)
	binary.Read(buf, binary.LittleEndian, &l2)
	binary.Read(buf, binary.LittleEndian, &l3)

	id := make([]byte, l1)
	action := make([]byte, l2)
	content := make([]byte, l3)
	binary.Read(buf, binary.LittleEndian, &id)
	binary.Read(buf, binary.LittleEndian, &action)
	binary.Read(buf, binary.LittleEndian, &content)
	return string(id), string(action), string(content)
}
